<?php
namespace app\admin\controller\nursinguser;;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use app\admin\model\publicuse\PublicUse;
use EasyAdmin\upload\Uploadfile;
use smartybuilder\libs\Quickbuilder;
use think\App;
/**
 * @ControllerAnnotation(title="课程设置")
 * Class Kecheng
 */
class Kecheng extends AdminController
{
    use \app\admin\traits\Curd;
     protected $allowModifyFields = [
        'status',
    ];
    protected $noExportFields = [
        'id',
        'name',
        'sort',
        'pid',
        'staff_grade_id',
        'videos',
        'video_href',
        'create_time',
        'update_time',
        'status',
        'delete_time',
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\StudyCourse();
        $array = PublicUse::getConfigDir(__CLASS__);
        if(is_file(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt')){
           $this->AllData = json_decode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt'),true);
        }
    }
    public function add()
    {
        $data = request()->param();
        if ($this->request->isAjax()) {
            $post = $this->request->post();
//            return $this->error('不从',$this->AllData);
            $rule = [];
            $this->validate($post, $rule);
//            return $this->error('保存失败2',$post);
            if(isset($this->AllData['field'])&&!empty($this->AllData['field'])){
                $post = PublicUse::Conversion($post,$this->AllData['field']);
            }
//            return $this->error('保存失败1',$post);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        if(isset($this->AllData['field'])){
            if($this->AllData['field']){
                $this->AllData['field'] = PublicUse::FieldGetLianJie($this->AllData['field']);
            }
//            echo '<pre>';
//            print_r($this->AllData['field']);
//            exit;
            if($this->AllData['tabletype']=='tree'){
                if(isset($this->AllData['field']['pid']['option'])&&$this->AllData['field']['pid']['option']){
                    $this->AllData['field']['pid']['option'] = PublicUse::getPidMenuList($this->AllData['field']['pid']['option']);
                }
            }
            $this->assign('alldata',$this->AllData);
        }
        if(isset($data['id'])){
            $shuju = $this->model->find($data['id']);
            if(!empty($shuju)){
                $data['staff_grade_id'] = $shuju->getData('staff_grade_id');
            }
        }
        if(isset($data['field'])&&isset($data['id'])&&$data['field']&&$data['id']){
            $data[$data['field']] = $data['id'];
            unset($data['field']);
            unset($data['id']);
        }
        $this->assign('row',$data);
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="设置视频")
     */
    public function shezi(){
        $id = $this->request->param('id');
        $data = $this->request->param();
        $shuju = $this->model->find($id);
        if(isset($data['video_href'])){
            try{
                $datas = [
                    'upload_type' => $this->request->post('upload_type'),
                    'file'        => $this->request->file('videos'),
                ];
                if(!empty($datas['file'])){
                    $uploadConfig = sysconfig('upload');
                    empty($datas['upload_type']) && $datas['upload_type'] = $uploadConfig['upload_type'];
                    $rule = [
                        'upload_type|指定上传类型有误' => "in:{$uploadConfig['upload_allow_type']}",
                        'file|文件'              => "require|file|fileExt:{$uploadConfig['upload_allow_ext']}|fileSize:{$uploadConfig['upload_allow_size']}",
                    ];
                    $this->validate($datas, $rule);
                    $upload = Uploadfile::instance()
                        ->setUploadType($datas['upload_type'])
                        ->setUploadConfig($uploadConfig)
                        ->setFile($datas['file'])
                        ->save();

                    $shuju->videos = $upload['url'];
                }
            }catch (\Exception $e){

            }
            $shuju->video_href = $data['video_href'];
            $shuju->cover = $data['cover'];
            $bool = $shuju->save(['update_time'=>time()]);
            if(!$bool){
                return $this->error('设置失败');
            }
            return $this->success('设置成功');
        }else{
            return Quickbuilder::makeForm()
                ->setAction()
                ->set_up_form('img','视频封面','cover',$shuju->getData('cover'),'','',1,[],'/admin/ajax/upload')
                ->set_up_form('video','视频','videos',$shuju->getData('videos'))
                ->set_up_form('input','视频地址','video_href',$shuju->getData('video_href'))
                ->set_button('submit','提交')
                ->fetch();
        }
    }
}